package com.uhylab.uhuvinetest.pager;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.uhylab.uhuvinetest.R;
import com.uhylab.uhuvinetest.UHApplication;
import com.uhylab.uhuvinetest.activity.UHPhotoActivity;
import com.uhylab.uhuvinetest.adapter.HistoryAdapter;
import com.uhylab.uhuvinetest.adapter.UHHistory_List_Adapter;
import com.uhylab.uhuvinetest.base.UHBase;
import com.uhylab.uhuvinetest.base.UHBasePager;
import com.uhylab.uhuvinetest.bean.Bean;
import com.uhylab.uhuvinetest.bean.UserHistory;
import com.uhylab.uhuvinetest.dao.CloudHistory;
import com.uhylab.uhuvinetest.dao.CloudHistoryDao;
import com.uhylab.uhuvinetest.dao.DaoSingleton;
import com.uhylab.uhuvinetest.dao.LocalHistory;
import com.uhylab.uhuvinetest.dao.LocalHistoryDao;
import com.uhylab.uhuvinetest.utils.VolleySingleton;
import com.uhylab.uhuvinetest.utils.sharedpreutils.SharedPreUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by leige07 .
 * Time on 2016/5/13 0013.
 * Description
 */
public class UHHistoryPager extends UHBasePager implements View.OnClickListener {

    private ListView history_list;
    private ImageView null_show;
    private VolleySingleton volleySingleton;
    private List<CloudHistory> data;
    private CloudHistoryDao cloudHistoryDao;
    private LocalHistoryDao localHistoryDao;
    private UHHistory_List_Adapter uhHistory_list_adapter;
    private List<Bean> list = new ArrayList<>();
    private HistoryAdapter adapter;


    public UHHistoryPager(Activity activity) {
        super(activity);
        cloudHistoryDao = DaoSingleton.getInstance().getCloudHistoryDao();
        localHistoryDao = DaoSingleton.getInstance().getLocalHistoryDao();
        volleySingleton = VolleySingleton.getVolleySingleton(UHApplication.gainContext());
    }

    @Override
    public void initData() {
        super.initData();
        View view = View.inflate(activity, R.layout.basepager_history,null);

        history_list = (ListView) view.findViewById(R.id.history_list);
        null_show = (ImageView) view.findViewById(R.id.null_show);

        null_show.setOnClickListener(this);
        history_list.setEmptyView(null_show);
        View headerView = LayoutInflater.from(activity).inflate(R.layout.fragment_history_list_head, null);
        history_list.addHeaderView(headerView, null, false);
        request();

        fl_child_content.addView(view);



    }

    private void request() {
        StringRequest stringRequest = new StringRequest(Request.Method.POST, UHBase.historyRecord, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    cloudHistoryDao.deleteAll();
                    Gson gson = new Gson();
                    UserHistory mUserHistory = gson.fromJson(response, UserHistory.class);
                    data = mUserHistory.getData();
                    for (int i = 0; i < data.size(); i++) {
                        cloudHistoryDao.insertOrReplace(data.get(i));
                    }
                    List<LocalHistory> localHistories = localHistoryDao.loadAll();

                    if (localHistories != null && localHistories.size() > 0)
                        for (int i = 0; i < localHistories.size(); i++) {

                            Bean bean = new Bean();
                            CloudHistory cloudHistory = new CloudHistory();
                            //日期
                            cloudHistory.setCreatetime(localHistories.get(i).getDate());
                            bean.setDate(localHistories.get(i).getDate());
                            //胆红素
                            cloudHistory.setBilirubin(localHistories.get(i).getBilirubin());
                            cloudHistory.setBilirubin_status(localHistories.get(i).getBilirubin_status());
                            bean.setPoint8(Float.parseFloat(localHistories.get(i).getBilirubin()));
                            //抗坏血酸
                            cloudHistory.setAscorbicacid(localHistories.get(i).getAscorbicacid());
                            cloudHistory.setAscorbicacid_status(localHistories.get(i).getAscorbicacid_status());
                            bean.setPoint10(Float.parseFloat(localHistories.get(i).getAscorbicacid()));

                            cloudHistory.setTelephone(localHistories.get(i).getTelephone());
                            //比重
                            cloudHistory.setGravity_status(localHistories.get(i).getGravity_status());
                            cloudHistory.setGravity(localHistories.get(i).getGravity());
                            bean.setPoint6(Float.parseFloat(localHistories.get(i).getGravity()));
                            //潜血
                            cloudHistory.setBlood(localHistories.get(i).getBlood());
                            cloudHistory.setBlood_status(localHistories.get(i).getBlood_status());
                            bean.setPoint5(Float.parseFloat(localHistories.get(i).getBlood()));
                            //葡萄糖
                            cloudHistory.setGlucose(localHistories.get(i).getGlucose());
                            cloudHistory.setGlucose_status(localHistories.get(i).getGlucose_status());
                            bean.setPoint9(Float.parseFloat(localHistories.get(i).getGlucose()));
                            //酮体
                            cloudHistory.setKetones(localHistories.get(i).getKetones());
                            cloudHistory.setKetones_status(localHistories.get(i).getKetones_status());
                            bean.setPoint7(Float.parseFloat(localHistories.get(i).getKetones()));
                            //白细胞
                            cloudHistory.setLeukocytes(localHistories.get(i).getLeukocytes());
                            cloudHistory.setLeukocytes_status(localHistories.get(i).getLeukocytes_status());
                            bean.setPoint0(Float.parseFloat(localHistories.get(i).getLeukocytes()));
                            //亚硝酸盐
                            cloudHistory.setNitrite(localHistories.get(i).getNitrite());
                            cloudHistory.setNitrite_status(localHistories.get(i).getNitrite_status());
                            bean.setPoint1(Float.parseFloat(localHistories.get(i).getNitrite()));
                            //酸碱度
                            cloudHistory.setPh(localHistories.get(i).getPh());
                            cloudHistory.setPh_status(localHistories.get(i).getPh_status());
                            bean.setPoint4(Float.parseFloat(localHistories.get(i).getPh()));
                            //蛋白质
                            cloudHistory.setProtein(localHistories.get(i).getProtein());
                            cloudHistory.setProtein_status(localHistories.get(i).getProtein_status());
                            bean.setPoint3(Float.parseFloat(localHistories.get(i).getProtein()));
                            //尿胆原
                            cloudHistory.setUrobilinogen(localHistories.get(i).getUrobilinogen());
                            cloudHistory.setUrobilinogen_status(localHistories.get(i).getUrobilinogen_status());
                            bean.setPoint2(Float.parseFloat(localHistories.get(i).getUrobilinogen()));

                            cloudHistoryDao.insertOrReplace(cloudHistory);
                            list.add(bean);
                        }

                    adapter = new HistoryAdapter(activity, list);
                    uhHistory_list_adapter = new UHHistory_List_Adapter(activity , cloudHistoryDao.queryRaw(""));
                    initAdapter();

                } catch (Exception e) {
                    Toast.makeText(activity , "获取历史记录失败", Toast.LENGTH_SHORT).show();
                }

            }


        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                List<LocalHistory> localHistories = localHistoryDao.loadAll();
                cloudHistoryDao.deleteAll();
                if (localHistories != null && localHistories.size() > 0)
                    for (int i = 0; i < localHistories.size(); i++) {
                        CloudHistory cloudHistory = new CloudHistory();
                        cloudHistory.setCreatetime(localHistories.get(i).getDate());
                        cloudHistory.setBilirubin(localHistories.get(i).getBilirubin());
                        cloudHistory.setBilirubin_status(localHistories.get(i).getBilirubin_status());
                        cloudHistory.setAscorbicacid(localHistories.get(i).getAscorbicacid());
                        cloudHistory.setAscorbicacid_status(localHistories.get(i).getAscorbicacid_status());
                        cloudHistory.setTelephone(localHistories.get(i).getTelephone());
                        cloudHistory.setGravity_status(localHistories.get(i).getGravity_status());
                        cloudHistory.setGravity(localHistories.get(i).getGravity());
                        cloudHistory.setBlood(localHistories.get(i).getBlood());
                        cloudHistory.setBlood_status(localHistories.get(i).getBlood_status());
                        cloudHistory.setGlucose(localHistories.get(i).getGlucose());
                        cloudHistory.setGlucose_status(localHistories.get(i).getGlucose_status());
                        cloudHistory.setKetones(localHistories.get(i).getKetones());
                        cloudHistory.setKetones_status(localHistories.get(i).getKetones_status());
                        cloudHistory.setLeukocytes(localHistories.get(i).getLeukocytes());
                        cloudHistory.setLeukocytes_status(localHistories.get(i).getLeukocytes_status());
                        cloudHistory.setNitrite(localHistories.get(i).getNitrite());
                        cloudHistory.setNitrite_status(localHistories.get(i).getNitrite_status());
                        cloudHistory.setPh(localHistories.get(i).getPh());
                        cloudHistory.setPh_status(localHistories.get(i).getPh_status());
                        cloudHistory.setProtein(localHistories.get(i).getProtein());
                        cloudHistory.setProtein_status(localHistories.get(i).getProtein_status());
                        cloudHistory.setUrobilinogen(localHistories.get(i).getUrobilinogen());
                        cloudHistory.setUrobilinogen_status(localHistories.get(i).getUrobilinogen_status());
                        cloudHistoryDao.insertOrReplace(cloudHistory);
                    }
                uhHistory_list_adapter = new UHHistory_List_Adapter(activity , cloudHistoryDao.loadAll());
                initAdapter();
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                String tel = SharedPreUtils.getString(activity , "user_tel", "");
                params.put("telephone", tel);
                return params;
            }
        };
        if (SharedPreUtils.getBoolean(activity , "is_Login", false)) {
            volleySingleton.addQueue(stringRequest, "stgRequest");
        }

    }

    private void initAdapter() {
        history_list.setAdapter(uhHistory_list_adapter);
//        history_list.setAdapter(adapter);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.null_show:
                Intent intent = new Intent(activity, UHPhotoActivity.class);
//                startActivity(intent);
                activity.startActivity(intent);
                break;
        }
    }
}
